package StringC;

/**
 * Created with Intellij IDEA.
 * Description;
 * User:TQ02
 * Data:2024-03-03
 * Time:11:55
 * Text：43,字符串相乘
 */
public class Text5 {
    public static String multiply(String num1, String num2) {
        int s1=num1.length()-1,s2=num2.length()-1;
        StringBuffer str=new StringBuffer();
        long num=0,y=0;
        //s1是num1的长度
        while(s1>=0){
            int k=s2;
            //count记录每一次的值。y1记录num的10几次方 n2记录的是超出的值
            long count=0,y1=0,n2=0;
            while(k>=0 || n2!=0){
                long cap=0;
                if(k>=0)
                    cap=(num1.charAt(s1)-48)*(num2.charAt(k) -48)+n2;
                else {
                    count=count+n2*(long)Math.pow(10,y1);
                    break;
                }
                n2=cap/10;
                count=count+cap%10*(int)Math.pow(10,y1);
                y1++;
                k--;
            }
            num=num+count*(int)Math.pow(10,y);
            y++;
            s1--;
        }
        if(num==0){
            return "0";
        }
        while (num!=0){
            str.append(num%10);
            num/=10;
        }
        return str.reverse().toString();
    }
    public static String multiply2(String num1, String num2) {
        //存放非进制位数字
        int s1 = num1.length(), s2 = num2.length();
        int[] arr = new int[s1 + s2 - 1];
        s1--;
        s2--;
        while (s1 >= 0) {
            int k = s2;
            while (k >= 0) {
                //求两个数之积
                int num = (num1.charAt(s1) - 48) * (num2.charAt(k) - 48);
                int x = s1 + k;
                arr[x] += num;
                k--;
            }
            s1--;
        }
        StringBuffer str = new StringBuffer();
        int count = 0;
        int l = arr.length - 1;
        while (l >= 0 || count != 0) {
            if (l >= 0) {
                str.append((arr[l]%10+count)%10);
            }else{
                str.append(count);
                break;
            }
            count=(arr[l--]+count)/10;
        }
        return str.reverse().toString();
    }

    public static void main(String[] args) {
        System.out.println(multiply2("66","128"));

    }
}
